package com.groupbuying.store.service.impl;

import com.groupbuying.store.connector.FileFegin;
import com.groupbuying.store.dao.StoreCheckoutCounterDao;
import com.groupbuying.store.domain.StoreCheckoutCounterDO;
import com.groupbuying.store.service.StoreCheckoutCounterService;
import com.groupbuying.store.utils.JSONUtils;
import com.groupbuying.store.utils.MultipartFileUtils;
import com.groupbuying.store.utils.QRCodeUtils;
import com.groupbuying.store.utils.WeiXinUtils;
import com.groupbuying.store.utils.apiresult.ApiResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service
public class StoreCheckoutCounterServiceImpl implements StoreCheckoutCounterService {
	@Autowired
	private StoreCheckoutCounterDao storeCheckoutCounterDao;

	@Autowired
	private WeiXinUtils weiXinUtils;

	@Autowired
	private QRCodeUtils qrCodeUtils;

	@Autowired
	private FileFegin fileFegin;

	@Override
	public StoreCheckoutCounterDO get(String uuid){
		return storeCheckoutCounterDao.get(uuid);
	}
	
	@Override
	public List<StoreCheckoutCounterDO> list(Map<String, Object> map){
		return storeCheckoutCounterDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return storeCheckoutCounterDao.count(map);
	}
	
	@Override
	public int save(StoreCheckoutCounterDO storeCheckoutCounter){
		return storeCheckoutCounterDao.save(storeCheckoutCounter);
	}
	
	@Override
	public int update(StoreCheckoutCounterDO storeCheckoutCounter){
		return storeCheckoutCounterDao.update(storeCheckoutCounter);
	}
	
	@Override
	public int remove(String uuid){
		return storeCheckoutCounterDao.remove(uuid);
	}
	
	@Override
	public int batchRemove(String[] uuids){
		return storeCheckoutCounterDao.batchRemove(uuids);
	}

    @Override
    public List<StoreCheckoutCounterDO> queryListInStoreUuid(Map map) {
		return storeCheckoutCounterDao.queryListInStoreUuid(map);
    }

	@Override
	public Map<String, Object> getStoreIdAndRuleIdByCounterId(Map<String, Object> params) {
		return storeCheckoutCounterDao.getStoreIdAndRuleIdByCounterId(params);
	}

	@Override
	public Map<String,String> saveCounterUnlimited(String counterUuid) {

		//获取token
		/*String accessToken = weiXinUtils.getAccessToken();

		//生成二维码
		byte[] b = weiXinUtils.getPayPageWxcode(accessToken,counterUuid);
		String payPage = getWxCodeFileGroupKey(counterUuid, b);
		b = weiXinUtils.getPaygyPageWxcode(accessToken,counterUuid);
		String paygyPage = getWxCodeFileGroupKey(counterUuid, b);*/

		byte[] b = qrCodeUtils.getPayPageQRCode(counterUuid);
		String payPage = getWxCodeFileGroupKey(counterUuid, b);
		b = qrCodeUtils.getPaygyPageQRCode(counterUuid);
		String paygyPage = getWxCodeFileGroupKey(counterUuid, b);

		Map<String,String> returnMap = new HashMap<>();
		returnMap.put("payPage",payPage);
		returnMap.put("paygyPage",paygyPage);
		return returnMap;
	}

	private String getWxCodeFileGroupKey(String counterUuid, byte[] b) {
		//TODO: 服务器路径问题
		//获取路径
		//String classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		//String command = "chmod 777 " + file.getAbsolutePath();
		//Runtime.getRuntime().exec(command);

		String classPath = "/tmp/";
		StringBuffer buffer = new StringBuffer();
		buffer.append(classPath);
		buffer.append("temp/");
		File file = new File(buffer.toString());
		if(!file.exists()){
			file.mkdir();
		}

		buffer.append(counterUuid);
		buffer.append(".png");
		String filePath = buffer.toString();

		System.out.println("完整路径" + filePath);

		file = new File(filePath);

		System.out.println(file);


		BufferedOutputStream bos = null;
		try {
			FileOutputStream fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(b);
			bos.flush();

			List<File> lsfile = new ArrayList<>();
			lsfile.add(file);
			String resultStr = fileFegin.rpcUploadFile(MultipartFileUtils.transferMultipartFiles(lsfile), "store");
			Map<String, Object> map = JSONUtils.jsonToMap(resultStr);
			if (map != null && "请求成功".equals(map.get("msg").toString())) {

				return JSONUtils.jsonToMap(map.get("result").toString()).get("fileGroupKey").toString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		    if(file.exists()){
		        file.delete();
            }
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}


}
